package com.matrix.cart.controller;

import com.matrix.api.cart.dto.CartItemDTO;
import com.matrix.api.cart.vo.CartResultVO;
import com.matrix.cart.service.CartService;
import com.matrix.common.domain.Result;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author 有点甜
 * @since 2025/8/8
 */
@RestController
@RequestMapping("/app/cart")
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;

    @GetMapping
    public Result<CartResultVO> getCart(@RequestHeader("X-User-ID") Long userId, Long shopId) {
        return Result.success(cartService.getCart(userId, shopId));
    }

    @PostMapping("/item")
    public Result<Void> addItem(@RequestHeader("X-User-ID") Long userId, @RequestBody @Valid CartItemDTO itemDTO) {
        cartService.addItem(userId, itemDTO);
        return Result.success();
    }

    @PutMapping("/item/quantity")
    public Result<Void> updateItemQuantity(@RequestHeader("X-User-ID") Long userId, @RequestBody @Valid CartItemDTO itemDTO) {
        cartService.updateItemQuantity(userId, itemDTO);
        return Result.success();
    }

    @DeleteMapping("/item")
    public Result<Void> removeItem(@RequestHeader("X-User-ID") Long userId, @RequestBody @Valid CartItemDTO itemKey) {
        cartService.removeItem(userId, itemKey);
        return Result.success();
    }

    @DeleteMapping
    public Result<Void> clearCart(@RequestHeader("X-User-ID") Long userId, Long shopId) {
        cartService.clearCart(userId, shopId);
        return Result.success();
    }
}
